package com.tools.developer.app;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Debug.MemoryInfo;

import com.tools.developer.util.CommonUtils;

public class AppUtil {
	public static ArrayList<AppModel> getRunningAllAppList(Context ctx) {

		ArrayList<AppModel> apps = new ArrayList<AppModel>();
		PackageManager pm = ctx.getPackageManager();

		List<PackageInfo> pkgInfos = CommonUtils.getRunningAllApps(ctx);
		AppModel model;
		for (PackageInfo pkg : pkgInfos) {
			model = new AppModel();
			String name = pkg.applicationInfo.loadLabel(pm).toString();

			model.packageName = pkg.packageName;
			model.mVer = pkg.versionName;
			model.mLabel = name;
			model.memUsage = getTotalPss(pkg.applicationInfo.uid, ctx);
			apps.add(model);
		}
		Collections.sort(apps, AppModel.SORT_APP_BY_LABEL);

		return apps;

	}

	public static PackageInfo getPackageInfo(Context ctx, String packageName) {
		PackageManager pm = ctx.getPackageManager();

		PackageInfo pkgInfo = null;
		try {
			pkgInfo = pm.getPackageInfo(packageName,
					PackageManager.GET_UNINSTALLED_PACKAGES);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

		return pkgInfo;
	}

	public static int getUid(Context ctx, String packageName) {

		PackageInfo pkgInfo = getPackageInfo(ctx, packageName);
		if (pkgInfo != null) {
			return pkgInfo.applicationInfo.uid;
		} else {
			return -1;
		}
	}

	public static ArrayList<AppModel> getInstalledAppList(Context ctx) {
		PackageManager pm = ctx.getPackageManager();
		List<PackageInfo> packages = pm.getInstalledPackages(0);
		ArrayList<AppModel> appList = new ArrayList<AppModel>();
		AppModel model;
		for (int i = 0; i < packages.size(); i++) {
			PackageInfo pack = packages.get(i);
			String name = pack.applicationInfo.loadLabel(pm).toString();
			String packageName = pack.applicationInfo.packageName;
			model = new AppModel();
			model.packageName = packageName;
			model.mLabel = name;
			model.mVer = pack.versionName;
			model.mShareUid = pack.sharedUserId;
			model.pkgInfo = pack;
			model.memUsage = getTotalPss(pack.applicationInfo.uid, ctx);
			appList.add(model);
		}

		Collections.sort(appList, AppModel.SORT_APP_BY_LABEL);

		return appList;
	}

	public static ArrayList<AppModel> getInstalledRomAppList(Context ctx) {
		PackageManager pm = ctx.getPackageManager();
		List<PackageInfo> packages = pm.getInstalledPackages(0);
		ArrayList<AppModel> appList = new ArrayList<AppModel>();
		AppModel model;

		for (int i = 0; i < packages.size(); i++) {
			PackageInfo pack = packages.get(i);
			if (pack != null) {
				ApplicationInfo appInfo = pack.applicationInfo;
				// 过滤非系统应用
				if (appInfo != null
						&& ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0)) {
					model = new AppModel();
					String name = appInfo.loadLabel(pm).toString();
					String packageName = appInfo.packageName;
					model.packageName = packageName;
					model.mLabel = name;
					model.mVer = pack.versionName;
					model.mShareUid = pack.sharedUserId;
					appList.add(model);
				}

			}
		}

		Collections.sort(appList, AppModel.SORT_APP_BY_LABEL);

		return appList;
	}

	/**
	 * 
	 * @param uid
	 * @param context
	 * @return Return total PSS memory usage in kB
	 */
	public static int getTotalPss(int uid, Context context) {
		android.os.Debug.MemoryInfo[] mems = getProcessMemoryInfo(uid, context);
		int memPSS = 0;
		if (mems == null) {
			return memPSS;
		}
		for (android.os.Debug.MemoryInfo mem : mems) {
			memPSS += mem.getTotalPss();
		}
		return memPSS;
	}

	public static MemoryInfo[] getProcessMemoryInfo(String packageName,
			Context context) {
		int uid = getUid(context, packageName);
		ActivityManager activityMgr = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		int[] pidArray = CommonUtils.getPids(uid, context);
		return activityMgr.getProcessMemoryInfo(pidArray);
	}

	public static MemoryInfo[] getProcessMemoryInfo(int uid, Context context) {
		ActivityManager activityMgr = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		int[] pidArray = CommonUtils.getPids(uid, context);
		return activityMgr.getProcessMemoryInfo(pidArray);
	}
}
